{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7.5 \u5b9a\u4e49\u6709\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u65b9\u6cd5\uff0c\u5b83\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684\u5e76\u4e14\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9a\u4e49\u4e00\u4e2a\u6709\u53ef\u9009\u53c2\u6570\u7684\u51fd\u6570\u662f\u975e\u5e38\u7b80\u5355\u7684\uff0c\u76f4\u63a5\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u7ed9\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5e76\u653e\u5230\u53c2\u6570\u5217\u8868\u6700\u540e\u5c31\u884c\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def spam(a, b=42):\n    print(a, b)\n\nspam(1) # Ok. a=1, b=42\nspam(1, 2) # Ok. a=1, b=2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u9ed8\u8ba4\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u4fee\u6539\u7684\u5bb9\u5668\u6bd4\u5982\u4e00\u4e2a\u5217\u8868\u3001\u96c6\u5408\u6216\u8005\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528None\u4f5c\u4e3a\u9ed8\u8ba4\u503c\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Using a list as a default value\ndef spam(a, b=None):\n    if b is None:\n        b = []\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5e76\u4e0d\u60f3\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u800c\u662f\u60f3\u4ec5\u4ec5\u6d4b\u8bd5\u4e0b\u67d0\u4e2a\u9ed8\u8ba4\u53c2\u6570\u662f\u4e0d\u662f\u6709\u4f20\u9012\u8fdb\u6765\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "_no_value = object()\n\ndef spam(a, b=_no_value):\n    if b is _no_value:\n        print('No b value supplied')\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6211\u4eec\u6d4b\u8bd5\u4e0b\u8fd9\u4e2a\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam(1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam(1, 2) # b = 2\nspam(1, None) # b = None"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ed4\u7ec6\u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\u5230\u4f20\u9012\u4e00\u4e2aNone\u503c\u548c\u4e0d\u4f20\u503c\u4e24\u79cd\u60c5\u51b5\u662f\u6709\u5dee\u522b\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9a\u4e49\u5e26\u9ed8\u8ba4\u503c\u53c2\u6570\u7684\u51fd\u6570\u662f\u5f88\u7b80\u5355\u7684\uff0c\u4f46\u7edd\u4e0d\u4ec5\u4ec5\u53ea\u662f\u8fd9\u4e2a\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e1c\u897f\u5728\u8fd9\u91cc\u4e5f\u6df1\u5165\u8ba8\u8bba\u4e0b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u4ec5\u4ec5\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u8d4b\u503c\u4e00\u6b21\u3002\u8bd5\u7740\u8fd0\u884c\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 42\ndef spam(a, b=x):\n    print(a, b)\nspam(1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 23 # Has no effect\nspam(1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u5230\u5f53\u6211\u4eec\u6539\u53d8x\u7684\u503c\u7684\u65f6\u5019\u5bf9\u9ed8\u8ba4\u53c2\u6570\u503c\u5e76\u6ca1\u6709\u5f71\u54cd\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u786e\u5b9a\u4e86\u5b83\u7684\u9ed8\u8ba4\u503c\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5176\u6b21\uff0c\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\u5e94\u8be5\u662f\u4e0d\u53ef\u53d8\u7684\u5bf9\u8c61\uff0c\u6bd4\u5982None\u3001True\u3001False\u3001\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u3002\n\u7279\u522b\u7684\uff0c\u5343\u4e07\u4e0d\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def spam(a, b=[]): # NO!\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8fd9\u4e48\u505a\u4e86\uff0c\u5f53\u9ed8\u8ba4\u503c\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u4fee\u6539\u540e\u4f60\u5c06\u4f1a\u9047\u5230\u5404\u79cd\u9ebb\u70e6\u3002\u8fd9\u4e9b\u4fee\u6539\u4f1a\u5f71\u54cd\u5230\u4e0b\u6b21\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\u7684\u9ed8\u8ba4\u503c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def spam(a, b=[]):\n    print(b)\n    return b\nx = spam(1)\nx"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x.append(99)\nx.append('Yow!')\nx"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam(1) # Modified list gets returned!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u7ed3\u679c\u5e94\u8be5\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f\uff0c\u6700\u597d\u662f\u5c06\u9ed8\u8ba4\u503c\u8bbe\u4e3aNone\uff0c\n\u7136\u540e\u5728\u51fd\u6570\u91cc\u9762\u68c0\u67e5\u5b83\uff0c\u524d\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u8fd9\u6837\u505a\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6d4b\u8bd5None\u503c\u65f6\u4f7f\u7528 is \u64cd\u4f5c\u7b26\u662f\u5f88\u91cd\u8981\u7684\uff0c\u4e5f\u662f\u8fd9\u79cd\u65b9\u6848\u7684\u5173\u952e\u70b9\u3002\n\u6709\u65f6\u5019\u5927\u5bb6\u4f1a\u72af\u4e0b\u4e0b\u9762\u8fd9\u6837\u7684\u9519\u8bef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def spam(a, b=None):\n    if not b: # NO! Use 'b is None' instead\n        b = []\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e48\u5199\u7684\u95ee\u9898\u5728\u4e8e\u5c3d\u7ba1None\u503c\u786e\u5b9e\u662f\u88ab\u5f53\u6210False\uff0c\n\u4f46\u662f\u8fd8\u6709\u5176\u4ed6\u7684\u5bf9\u8c61(\u6bd4\u5982\u957f\u5ea6\u4e3a0\u7684\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49)\u90fd\u4f1a\u88ab\u5f53\u505aFalse\u3002\n\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8bef\u5c06\u4e00\u4e9b\u5176\u4ed6\u8f93\u5165\u4e5f\u5f53\u6210\u662f\u6ca1\u6709\u8f93\u5165\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam(1) # OK\nx = []\nspam(1, x) # Silent error. x value overwritten by default\nspam(1, 0) # Silent error. 0 ignored\nspam(1, '') # Silent error. '' ignored"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u95ee\u9898\u6bd4\u8f83\u5fae\u5999\uff0c\u90a3\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u6d4b\u8bd5\u67d0\u4e2a\u53ef\u9009\u53c2\u6570\u662f\u5426\u88ab\u4f7f\u7528\u8005\u4f20\u9012\u8fdb\u6765\u3002\n\u8fd9\u65f6\u5019\u9700\u8981\u5c0f\u5fc3\u7684\u662f\u4f60\u4e0d\u80fd\u7528\u67d0\u4e2a\u9ed8\u8ba4\u503c\u6bd4\u5982None\u3001\n0\u6216\u8005False\u503c\u6765\u6d4b\u8bd5\u7528\u6237\u63d0\u4f9b\u7684\u503c(\u56e0\u4e3a\u8fd9\u4e9b\u503c\u90fd\u662f\u5408\u6cd5\u7684\u503c\uff0c\u662f\u53ef\u80fd\u88ab\u7528\u6237\u4f20\u9012\u8fdb\u6765\u7684)\u3002\n\u56e0\u6b64\uff0c\u4f60\u9700\u8981\u5176\u4ed6\u7684\u89e3\u51b3\u65b9\u6848\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u79c1\u6709\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5c31\u50cf\u4e0a\u9762\u7684_no_value\u53d8\u91cf\u90a3\u6837\u3002\n\u5728\u51fd\u6570\u91cc\u9762\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u88ab\u4f20\u9012\u53c2\u6570\u503c\u8ddf\u8fd9\u4e2a\u5b9e\u4f8b\u662f\u5426\u4e00\u6837\u6765\u5224\u65ad\u3002\n\u8fd9\u91cc\u7684\u601d\u8def\u662f\u7528\u6237\u4e0d\u53ef\u80fd\u53bb\u4f20\u9012\u8fd9\u4e2a_no_value\u5b9e\u4f8b\u4f5c\u4e3a\u8f93\u5165\u3002\n\u56e0\u6b64\uff0c\u8fd9\u91cc\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e2a\u503c\u5c31\u80fd\u786e\u5b9a\u67d0\u4e2a\u53c2\u6570\u662f\u5426\u88ab\u4f20\u9012\u8fdb\u6765\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u5bf9 object() \u7684\u4f7f\u7528\u770b\u4e0a\u53bb\u6709\u70b9\u4e0d\u592a\u5e38\u89c1\u3002object \u662fpython\u4e2d\u6240\u6709\u7c7b\u7684\u57fa\u7c7b\u3002\n\u4f60\u53ef\u4ee5\u521b\u5efa object \u7c7b\u7684\u5b9e\u4f8b\uff0c\u4f46\u662f\u8fd9\u4e9b\u5b9e\u4f8b\u6ca1\u4ec0\u4e48\u5b9e\u9645\u7528\u5904\uff0c\u56e0\u4e3a\u5b83\u5e76\u6ca1\u6709\u4efb\u4f55\u6709\u7528\u7684\u65b9\u6cd5\uff0c\n\u4e5f\u6ca1\u6709\u4efb\u4f55\u5b9e\u4f8b\u6570\u636e(\u56e0\u4e3a\u5b83\u6ca1\u6709\u4efb\u4f55\u7684\u5b9e\u4f8b\u5b57\u5178\uff0c\u4f60\u751a\u81f3\u90fd\u4e0d\u80fd\u8bbe\u7f6e\u4efb\u4f55\u5c5e\u6027\u503c)\u3002\n\u4f60\u552f\u4e00\u80fd\u505a\u7684\u5c31\u662f\u6d4b\u8bd5\u540c\u4e00\u6027\u3002\u8fd9\u4e2a\u521a\u597d\u7b26\u5408\u6211\u7684\u8981\u6c42\uff0c\u56e0\u4e3a\u6211\u5728\u51fd\u6570\u4e2d\u5c31\u53ea\u662f\u9700\u8981\u4e00\u4e2a\u540c\u4e00\u6027\u7684\u6d4b\u8bd5\u800c\u5df2\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}